//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package admin_docker_template_api_serv

import (
	"context"
	"time"

	"atomgit.com/openlinksaas/api-server/admin_api_serv/admin_common"
	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/docker_dao"
	"atomgit.com/openlinksaas/api-server/dao/fs_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/docker_template_api"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *DockerTemplateAdminImpl) CreateTemplate(ctx context.Context, req *docker_template_api.AdminCreateTemplateRequest) (*docker_template_api.AdminCreateTemplateResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminCreateTemplateResponse{
			Code:   docker_template_api.AdminCreateTemplateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminCreateTemplateResponse{
			Code:   docker_template_api.AdminCreateTemplateResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.CreateTemplate {
		return &docker_template_api.AdminCreateTemplateResponse{
			Code:   docker_template_api.AdminCreateTemplateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	//检查应用是否存在
	exist, err := docker_dao.AppDao.Exist(ctx, req.AppId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminCreateTemplateResponse{
			Code:   docker_template_api.AdminCreateTemplateResponse_CODE_NO_APP,
			ErrMsg: "应用不存在",
		}, nil
	}
	//检查模板是否存在
	exist, err = docker_dao.TemplateDao.Exist(ctx, req.AppId, req.Version)
	if err != nil {
		return nil, err
	}
	if exist {
		return &docker_template_api.AdminCreateTemplateResponse{
			Code:   docker_template_api.AdminCreateTemplateResponse_CODE_EXIST_TEMPLATE,
			ErrMsg: "模板已经存在",
		}, nil
	}

	//检查文件是否存在
	dtFsId, err := fs_dao.GlobalFsDao.GetDockerTemplateFs(ctx)
	if err != nil {
		return nil, err
	}
	exist, err = fs_dao.FsFileDao.Exist(ctx, dtFsId, req.FileId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminCreateTemplateResponse{
			Code:   docker_template_api.AdminCreateTemplateResponse_CODE_NO_FILE,
			ErrMsg: "文件不存在",
		}, nil
	}

	nowTime := time.Now().UnixNano() / 1e6

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.TemplateDao.Insert(sessCtx, &docker_dao.TemplateInfoDbItem{
			AppId:     req.AppId,
			Version:   req.Version,
			FileId:    req.FileId,
			TimeStamp: nowTime,
		})
		if err != nil {
			return nil, err
		}
		count, err := docker_dao.TemplateDao.Count(sessCtx, req.AppId)
		if err != nil {
			return nil, err
		}
		err = docker_dao.AppDao.UpdateTemplateCount(sessCtx, req.AppId, count)
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminCreateTemplateResponse{
			Code: docker_template_api.AdminCreateTemplateResponse_CODE_OK,
		}, nil
	})

	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminCreateTemplateResponse), nil
}

func (apiImpl *DockerTemplateAdminImpl) RemoveTemplate(ctx context.Context, req *docker_template_api.AdminRemoveTemplateRequest) (*docker_template_api.AdminRemoveTemplateResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminRemoveTemplateResponse{
			Code:   docker_template_api.AdminRemoveTemplateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminRemoveTemplateResponse{
			Code:   docker_template_api.AdminRemoveTemplateResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.RemoveTemplate {
		return &docker_template_api.AdminRemoveTemplateResponse{
			Code:   docker_template_api.AdminRemoveTemplateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	//检查应用是否存在
	exist, err := docker_dao.AppDao.Exist(ctx, req.AppId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminRemoveTemplateResponse{
			Code:   docker_template_api.AdminRemoveTemplateResponse_CODE_NO_APP,
			ErrMsg: "应用不存在",
		}, nil
	}
	//检查模板是否存在
	exist, err = docker_dao.TemplateDao.Exist(ctx, req.AppId, req.Version)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminRemoveTemplateResponse{
			Code:   docker_template_api.AdminRemoveTemplateResponse_CODE_NO_TEMPLATE,
			ErrMsg: "模板不存在",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.TemplateDao.Remove(sessCtx, req.AppId, req.Version)
		if err != nil {
			return nil, err
		}
		count, err := docker_dao.TemplateDao.Count(sessCtx, req.AppId)
		if err != nil {
			return nil, err
		}
		err = docker_dao.AppDao.UpdateTemplateCount(sessCtx, req.AppId, count)
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminRemoveTemplateResponse{
			Code: docker_template_api.AdminRemoveTemplateResponse_CODE_OK,
		}, nil
	})

	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminRemoveTemplateResponse), nil
}
